package cn.wjdiankong.levideo.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import cn.wjdiankong.levideo.R;
import cn.wjdiankong.levideo.data.LevideoData;
import cn.wjdiankong.levideo.database.LocalStorage;
import cn.wjdiankong.levideo.dialog.ConfirmDialog;
import cn.wjdiankong.levideo.dialog.VideoInfoDialog;
import cn.wjdiankong.levideo.dialog.VideoLongClickPopupMenu;
import cn.wjdiankong.levideo.dialog.VideoLongClickPopupMenu.MenuClickItemListener;
import cn.wjdiankong.levideo.list.adapter.ToutiaoVideoListAdapter;
import cn.wjdiankong.levideo.utils.Const;
import cn.wjdiankong.levideo.utils.FileUtils;
import cn.wjdiankong.levideo.utils.UIUtil;
import cn.wjdiankong.levideo.utils.UmengReportUtils;
import cn.wjdiankong.levideo.utils.Utils;
import cn.wjdiankong.levideo.view.SmileyLoadingView;

@SuppressLint("NewApi")
public class ToutiaoVideoActivity extends BaseActivity {

	private SmileyLoadingView loadingView;
	private View noDataView, contentView;
	private ListView mListView;
	private ToutiaoVideoListAdapter mAdapter;
	private VideoLongClickPopupMenu menu;
	private VideoInfoDialog infoDialog;
	
	private List<LevideoData> toutiaoList = new ArrayList<LevideoData>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_toutiao_localvideo);
		init();
	}
	
	private void init(){
		initView();
		initData();
	}
	
	protected void initView(){
		super.initView();
		loadingView = (SmileyLoadingView) findViewById(R.id.loading_view);
		noDataView = findViewById(R.id.nodata_layout);
		contentView = findViewById(R.id.content_view);
		mListView = (ListView)findViewById(R.id.listview);
		setTitle("头条视频");
		setLeftImg(R.drawable.back_icon);
		showLoadingView();
	}

	protected void initData(){
		super.initData();
		mAdapter = new ToutiaoVideoListAdapter(getApplicationContext());
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				LevideoData data = mAdapter.getDataList().get(position);
				Uri uri = Uri.parse("file://"+data.videoPlayUrl);
				data.videoPlayUrl = uri.toString();
				VideoPlayerActivity.startDefaultResult(ToutiaoVideoActivity.this, 
						(ArrayList<LevideoData>)mAdapter.getDataList(),
						position, Const.VIDEO_TYPE_TOUTIAO, true);
			}
		});
		mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				UmengReportUtils.report(UmengReportUtils.TOUTIAO_VIDEO_MENU);
				final LevideoData data = mAdapter.getDataList().get(position);
				if(menu == null){
					menu = new VideoLongClickPopupMenu(ToutiaoVideoActivity.this);
				}
				menu.setMenuShowAnchorView(view, mListView, position);
				menu.setOneScreen(true);
				menu.setMenuClickItemListener(new MenuClickItemListener() {
					@Override
					public void clickItem(int type) {
						if(type == MenuClickItemListener.DELETE_VIDEO){
							UmengReportUtils.report(UmengReportUtils.TOUTIAO_VIDEO_DELETE);
							//对于头条视频，数据库中的保存数据和firstVideoPlayUrl值就是一样的。
							boolean isSucc = LocalStorage.getInstance().deleteLevideoData(data.videoPlayUrl);
							if(isSucc){
								File file = new File(data.videoPlayUrl);
								if(file.exists()){
									isSucc = file.delete();
									if(isSucc){
										if(mAdapter.getDataList().size() == 1){
											//删除完了
											showNoDataView();
										}
										mAdapter.removeData(data);
										UIUtil.showShortHintToast(getApplicationContext(), "删除成功");
									}
								}
							}
						}else if(type == MenuClickItemListener.DETAIL_VIDEO){
							UmengReportUtils.report(UmengReportUtils.TOUTIAO_VIDEO_DETAIL);
							if(infoDialog == null){
								infoDialog = new VideoInfoDialog(ToutiaoVideoActivity.this);
							}
							infoDialog.setLevideoData(data);
							infoDialog.show();
						}
					}
				});
				menu.toggleMenu();
				return true;
			}
		});
		
		new AsyncTask<Void, Void, Void>(){
			@Override
			protected Void doInBackground(Void... params) {
				getLocalData();
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				super.onPostExecute(result);
				if(toutiaoList != null && toutiaoList.size() > 0){
					mAdapter.setDataList(toutiaoList);
					showContentView();
				}else{
					showNoDataView();
				}
			}
		}.execute();
	}
	
	private void getLocalData(){
		List<LevideoData> toutiaoVideoDataList = LocalStorage.getInstance().getLevideoDataList(Const.VIDEO_TYPE_TOUTIAO);
		File[] toutiaoFiles = FileUtils.getLocalVideo(Const.VIDEO_TYPE_TOUTIAO);
		if(toutiaoVideoDataList != null && toutiaoVideoDataList.size() > 0 && toutiaoFiles.length > 0){
			for(File file : toutiaoFiles){
				String name = file.getAbsolutePath();
				for(LevideoData data : toutiaoVideoDataList){
					if(name.equals(data.videoPlayUrl)){
						data.formatTimeStr = Utils.formatTimeStr(data.createTime);
						data.title = mAdapter.formatContent(data.title);
						toutiaoList.add(data);
					}
				}
			}
		}
		if(toutiaoList.size() > 0){
			Utils.sortLevideoData(toutiaoList);
		}
	}
	
	private void showLoadingView(){
		loadingView.start();
		loadingView.setVisibility(View.VISIBLE);
		contentView.setVisibility(View.GONE);
		noDataView.setVisibility(View.GONE);
	}
	
	private void showContentView(){
		loadingView.stop();
		loadingView.setVisibility(View.GONE);
		contentView.setVisibility(View.VISIBLE);
		noDataView.setVisibility(View.GONE);
		setRightImg(R.drawable.delete_icon);
	}
	
	private void showNoDataView(){
		loadingView.stop();
		loadingView.setVisibility(View.GONE);
		contentView.setVisibility(View.GONE);
		noDataView.setVisibility(View.VISIBLE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode == VideoPlayerActivity.CODE_RESULT){
			int position = data.getIntExtra("del_position", -1);
			if(position >= 0){
				if(mAdapter.getDataList().size() == 1){
					//删除完了
					showNoDataView();
				}
				mAdapter.removeData(position);
				UIUtil.showShortHintToast(getApplicationContext(), "删除成功");
			}
		}
	}
	
	public static void startDefault(Context context){
		Intent intent = new Intent(context, ToutiaoVideoActivity.class);
		context.startActivity(intent);
	}
	
	@Override
	protected void clickLeftImg(){
		finish();
	}
	
	@Override
	protected void clickRightImg(){
		
		final ConfirmDialog dialog = new ConfirmDialog(this);
		dialog.setContentTxt("您将删除所有下载的头条视频，是否确定继续操作？").setLeftTxt("取消", new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dimiss();
			}
		}).setRightTxt("确定", new OnClickListener() {
			@Override
			public void onClick(View v) {
				//删除全部
				boolean isSucc = FileUtils.isDeleteSucc(Const.VIDEO_TYPE_TOUTIAO);
				if(isSucc){
					UIUtil.showShortHintToast(getApplicationContext(), "删除成功");
				}else{
					UIUtil.showShortHintToast(getApplicationContext(), "删除失败");
				}
			}
		}).show();
		
		UmengReportUtils.report(UmengReportUtils.TOUTIAO_VIDEO_DELETE_ALL);
	}
	
	@Override
	protected String getPageName() {
		return "toutiao_video";
	}
	
}